package com.portal.cwegportal.service;

import com.appcenter.jz.app.CurrentUser;
import com.portal.cwegportal.mapper.ReleaseScopeMapper;
import com.portal.cwegportal.model.ReleaseScope;


import com.portal.cwegportal.model.Tool.PrivilegeTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 *  创建人:王增立
 *  备注: 根据orgPath获取发布范围集合
 *  时间: 2019/4/26 15:21
 *  参数: 
 */
@Service
public class PrivilegeService {
    @Autowired
    ReleaseScopeMapper releaseScopeMapper;
    /**
     *  创建人:王增立
     *  备注: 根据过滤方法，根据orgPath获取发布范围集合
     *  时间: 2019/4/26 15:22
     *  参数: 
     */
    public PrivilegeTool GetPrivilegeByOrgPath(String publishType){
        PrivilegeTool Pt=new PrivilegeTool();
        boolean IsAdmin = false;//是否是相應類型的管理員
        boolean IsSubAdmin = false;//是否是超級管理員
        if(!CurrentUser.getRoleLists().toString().equals("")&&CurrentUser.getRoleLists()!=null) {
            //1.先根据类型判断角色是否为管理员
            ArrayList<String> RoleList = (ArrayList<String>) CurrentUser.getRoleLists();
            //1.新闻 2 公告 3发文
            String AdminName = "";
            String SubAdminName = "系统管理员";
            switch (publishType) {
                case "1":
                    AdminName = "新闻管理员";
                    break;
                case "2":
                    AdminName = "公告管理员";
                    break;
                case "3":
                    AdminName = "发文管理员";
                    break;
                default:
                    AdminName = "";
            }
            for (int i = 0; i < RoleList.size(); i++) {
                if (SubAdminName.equals(RoleList.get(i).toString())) {
                    IsSubAdmin = true;
                    break;
                }
                if (AdminName.equals(RoleList.get(i).toString()) && AdminName != "") {
                    IsAdmin = true;
                }
            }
        }
        //2.非管理员根据路径判断权限
        ArrayList< HashMap<String,Object> > OrgPath=(ArrayList< HashMap<String,Object> >)CurrentUser.getOrgs();
        String OrgIdPath="";
        for (HashMap<String,Object> m : OrgPath) {
            for (String key:m.keySet()){
                if(key.equals("OrgIdPath")){
                    OrgIdPath=m.get(key).toString();
                }
            }
        }

        List<ReleaseScope> Relist= releaseScopeMapper.selectByPrivilege(OrgIdPath,publishType);
        List<Long> ReleaseScopeIdTypes=new ArrayList<Long>();
        long kk=0;
        for (int i=0;i< Relist.size();i++){
            if(!ReleaseScopeIdTypes.contains(Relist.get(i).getKeyId())){
               ReleaseScopeIdTypes.add(Relist.get(i).getKeyId());
            }
        }

        //组装返回内容
        Pt.setErre(false);
        Pt.setAdmin(false);
        if(IsAdmin||IsSubAdmin){
            Pt.setAdmin(true);
        }
        if(!IsAdmin&&IsSubAdmin&&(OrgPath.size()==0||OrgPath==null)){
            Pt.setErre(true);
        }
        Pt.setOrgIdPath(OrgIdPath);
        Pt.setPrivilegeLiat(ReleaseScopeIdTypes);
        return Pt;
    }
}
